var t = require("../../../@babel/runtime/helpers/interopRequireDefault"), e = t(require("../../../dist/dialog/dialog")), i = t(require("../../../utils/polyvlive.js")), o = require("../../../utils/CryptoJS.js"), a = require("../../../mycomponent/yz_pay/yz_pay"), s = getApp();

Component({
    behaviors: [ a ],
    properties: {
        prop_goods_info: {
            type: Object,
            value: {}
        },
        goodsId: {
            type: Number
        },
        goodsType: {
            type: String
        },
        mark: {
            type: String
        },
        mark_id: {
            type: String
        },
        ipnonexbol: {
            type: null
        },
        is_open: {
            type: null
        }
    },
    observers: {
        is_open: function(t) {
            this.setData({
                questionnaire_is_open: "close" != t && ""
            });
        }
    },
    data: {
        is_try_time: null,
        is_questionnaire_time: null,
        questionnaire_is_open: !1,
        language: "",
        goods_id: "",
        is_show: !0,
        is_vshow: !1,
        isVideo: !0,
        isLook: !0,
        is_update: !1,
        is_buy: !1,
        showPopup: !1,
        rewardMoneyBtn: !1,
        actionSheetShow: !1,
        rewardBtnShow: !1,
        rewardMoney: "",
        goodTit: "",
        goodPrice: "",
        chapterNum: "",
        teacherImg: "",
        teacherName: "",
        goodsContent: "",
        chapterList: [],
        article_content: "",
        courseShare: {},
        courseData: {},
        codeUrl: "",
        vedioSrc: "",
        coverImg: "",
        active: 1,
        chapter_id: "",
        courseId: "",
        pushGoodList: [],
        actionSheetItems: [],
        isLoding: !1,
        activeNames: 1,
        chapterdigList: [],
        vip_level_status: {},
        islive: !1,
        video_err: !1,
        is_playLive: !1,
        live: !1,
        videoErr: !0,
        ios_virtual_pay: !1,
        point_goods: "",
        integral: "积分",
        show1: !1,
        posterShow: !1,
        styleSetOptions: {},
        countDownTime: null,
        timeData: {},
        iscCountDownFinish: !1,
        HasBuyPermissions: !1,
        videoEventDom: null,
        viewingDuration: 0,
        active_tab: 0,
        goods_plugin: null
    },
    lifetimes: {
        attached: function(t) {
            wx.setNavigationBarTitle({
                title: "课程详情"
            });
            var e = wx.getStorageSync("langIndex");
            this.setData({
                language: e.en
            }), this.setData({
                goods_id: this.data.goodsId
            }), this._getData(), this.footSet();
            try {
                var i = wx.getStorageSync("ios_virtual_pay");
                i && this.setData({
                    ios_virtual_pay: i
                });
            } catch (t) {
                console.log(t);
            }
            try {
                var o = wx.getStorageSync("yz_basic_info");
                this.setData({
                    shopName: o.home.mailInfo.name
                });
            } catch (t) {
                console.log(t);
            }
            this.data.prop_goods_info.plugin && this.data.prop_goods_info.plugin.point_goods && this.setData({
                point_goods: this.data.prop_goods_info.plugin.point_goods
            }), wx.setStorageSync("GoddsShareImg", this.data.prop_goods_info.get_goods.thumb);
            var a = wx.getStorageSync("integral");
            a && this.setData({
                integral: a
            });
        },
        detached: function() {
            this.data.is_try_time = null, this.data.is_questionnaire_time = null, clearTimeout(this.data.is_try_time), 
            clearTimeout(this.data.is_questionnaire_time);
        }
    },
    pageLifetimes: {
        hide: function() {
            this.data.is_try_time = null, this.data.is_questionnaire_time = null, clearTimeout(this.data.is_try_time), 
            clearTimeout(this.data.is_questionnaire_time);
        }
    },
    methods: {
        change_tab: function(t) {
            console.log(t), this.setData({
                active_tab: t.detail.index
            });
        },
        handleContact: function(t) {
            var e = t.currentTarget.dataset.url;
            e && wx.navigateTo({
                url: e,
                fail: function(t) {
                    s.tips("客服路径错误");
                }
            });
        },
        tapkefu: function() {
            this.setData({
                show1: !0
            });
        },
        callPhone: function() {
            wx.makePhoneCall({
                phoneNumber: this.data.service_mobile
            });
        },
        closePop: function() {
            this.setData({
                show1: !1
            });
        },
        changeLoading: function() {
            this.setData({
                isLoding: !0
            });
        },
        setPosterShow: function() {
            this.setData({
                posterShow: !this.data.posterShow
            });
        },
        videoErr: function() {
            wx.showToast({
                title: "视频加载出错了！",
                icon: "none",
                duration: 2e3
            }), this.setData({
                videoErr: !1
            });
        },
        getQrCode: function() {
            var t = this;
            s._getNetWork({
                url: s.getNetAddresss("member.member.getArticleQr"),
                success: function(e) {
                    if (1 != e.data.result || s._isTextEmpty(e.data.data)) console.error(e); else {
                        var i = e.data.data;
                        t.setData({
                            codeUrl: i
                        });
                    }
                },
                fail: function(t) {
                    console.error(t);
                }
            });
        },
        secret: function(t, e, i) {
            e = o.MD5(e).toString();
            var a = o.enc.Utf8.parse(e.substring(0, 16)), s = o.enc.Utf8.parse(e.substring(16));
            return i ? o.AES.decrypt(t, s, {
                iv: a,
                padding: o.pad.Pkcs7
            }).toString(o.enc.Utf8) : o.AES.encrypt(t, s, {
                iv: a,
                mode: o.mode.CBC,
                padding: o.pad.Pkcs7
            }).toString();
        },
        addzhibo: function() {
            var t = this;
            s._getNetWork({
                url: s.getNetAddresss("plugin.video-demand.api.video-course-goods.get-live-info"),
                data: {
                    goods_id: t.data.goods_id
                },
                success: function(e) {
                    if (1 == e.data.result) {
                        if (1 == t.data.islive) return void wx.showToast({
                            title: "正在直播",
                            icon: "none",
                            duration: 1e3
                        });
                        var i = JSON.parse(t.secret(e.data.data, "zoQUA@257ggflNmMppKzcU7QgYyp!tU&h54VNN3u!PAU$8FxBW", !0));
                        t.is_onlive(i.userId, i.channelId), t.setData({
                            islive: !0
                        });
                    } else wx.showToast({
                        title: e.data.msg,
                        icon: "none",
                        duration: 1e3
                    }), t.setData({
                        is_update: !0,
                        is_buy: !0
                    });
                },
                fail: function(t) {
                    console.error(t);
                }
            });
        },
        is_onlive: function(t, e) {
            var o = this;
            wx.showLoading({
                title: "加载直播中",
                icon: "none"
            });
            var a = wx.createLivePlayerContext("polyvLiveVideo");
            this.setData({
                islive: !0
            }), i.default.getVideo({
                uid: t,
                vid: e,
                videoContext: a,
                success: function(t) {
                    console.log(t), o.setPlayerSrc(t);
                },
                error: function(t) {
                    console.log(t.error);
                },
                onStartLive: function() {
                    console.log("start live"), wx.hideLoading(), o.setNewPlayerSrc();
                },
                onApiStatus: function(t) {
                    console.log(t), "end" == t ? (o.setData({
                        is_playLive: !1,
                        islive: !1,
                        videoErr: !0,
                        is_vshow: !0
                    }), wx.hideLoading(), wx.showToast({
                        title: "当前无直播",
                        icon: "none",
                        duration: 1e3
                    }), i.default.destroy()) : "live" == t && o.setData({
                        is_playLive: !0
                    });
                }
            });
        },
        setPlayerSrc: function(t) {
            this.setData({
                video: {
                    src: t.src,
                    poster: t.poster
                },
                is_bisrc: t.src
            }), console.log(t.src);
        },
        setNewPlayerSrc: function() {
            var t = this;
            i.default.getNewVideo(function(e) {
                t.setPlayerSrc(e);
            });
        },
        onShareAppMessage: function(t) {
            var e = wx.getStorageSync("yz_uid"), i = "";
            e && (i = e);
            var o = s._isTextEmpty(this.data.courseShare.share_title) ? this.data.goodTit : this.data.courseShare.share_title, a = "/packageA/detail_v2/detail_v2?id=" + this.data.chapter_id + "&mid=" + i, n = s._isTextEmpty(this.data.courseShare.share_thumb) ? this.data.courseData.has_one_goods.thumb : this.data.courseShare.share_thumb;
            return {
                title: o,
                desc: this.data.courseShare.share_desc,
                imageUrl: n,
                path: a,
                success: function(t) {
                    console.info(t + "成功");
                },
                fail: function(t) {
                    console.log(t + "失败");
                },
                complete: function(t) {}
            };
        },
        goSurvey: function() {
            wx.navigateTo({
                url: "/packageE/questionnaire/questions/questions?goods_id=" + this.data.goods_id + "&tag=video"
            });
        },
        _getData: function() {
            var t = this;
            s._getNetWork({
                url: s.getNetAddresss("plugin.video-demand.api.video-course-goods.get-course-goods-detail"),
                data: {
                    goods_id: t.data.goods_id
                },
                success: function(i) {
                    if (1 === i.data.result) {
                        var o, a, n = i.data.data;
                        if (s._isTextEmpty(n.has_many_dig_chapter) || 0 == n.has_many_dig_chapter.length) return void e.default.alert({
                            title: "提示",
                            message: "课程已下架！",
                            context: t
                        }).then(function() {
                            wx.navigateBack({
                                delta: 1
                            });
                        });
                        t.setData({
                            chapterdigList: n.has_many_dig_chapter
                        }), n.has_one_goods.content = n.has_one_goods.content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block" ');
                        for (var r = "", d = 0, u = n.has_many_dig_chapter, c = 0; c < u.length; c++) if (u[c].has_many_chapter.length > 0 && t.data.chapter_id) for (var h = 0; h < u[c].has_many_chapter.length; h++) u[c].has_many_chapter[h].id === Number(t.data.chapter_id) && (r = u[c].has_many_chapter[h], 
                        d = h); else d = 0, r = u[0].has_many_chapter[0];
                        var l = "";
                        l = t.data.point_goods ? t.data.point_goods.point : n.has_one_goods.price;
                        var _ = n.course_expiration_time ? s.getTimeDifference(n.course_expiration_time) : null;
                        t.setData({
                            courseData: n,
                            rewardBtnShow: n.is_reward,
                            goodTit: n.has_one_goods.title,
                            goodPrice: l,
                            vip_level_status: n.has_one_goods.vip_level_status,
                            chapterNum: n.course_chapter_num,
                            teacherImg: n.has_one_lecturer && n.has_one_lecturer.has_one_member ? n.has_one_lecturer.has_one_member.avatar : "",
                            teacherName: n.has_one_lecturer && n.has_one_lecturer.real_name || "",
                            goodsContent: n.has_one_goods.content,
                            chapter_id: r && r.id || 0,
                            isLook: r && r.is_audition || !0,
                            courseId: n.id,
                            pushGoodList: n.pushGoods,
                            courseShare: n.has_one_goods.has_one_share,
                            live: n.live,
                            live_title: n.live_title,
                            questionnaire_time: n.questionnaire_time,
                            service_QRcode: n.shopSet.service_QRcode,
                            service_mobile: n.shopSet.service_mobile,
                            shopSet: n.shopSet,
                            plugin_point_name: n.plugin_point_name ? n.plugin_point_name : "",
                            styleSetOptions: n.styleSet,
                            countDownTime: _,
                            HasBuyPermissions: n.buyPermissions || !1,
                            advertising: null !== (o = null == n || null === (a = n.has_one_goods) || void 0 === a ? void 0 : a.has_one_goods_advertising) && void 0 !== o ? o : {},
                            goods_plugin: n.plugin
                        });
                        var g = 1e3 * n.questionnaire_time;
                        switch (n.questionnaire_is_open && ("close" == t.data.is_opens ? console.log(t.data.is_opens) : t.data.is_questionnaire_time = setTimeout(function() {
                            t.data.is_questionnaire_time && t.setData({
                                questionnaire_is_open: n.questionnaire_is_open
                            });
                        }, g)), n.watch) {
                          case 0:
                            t.setData({
                                is_update: !1,
                                is_buy: !1
                            }), t.videoInit(d, r);
                            break;

                          case 1:
                            if (1 == r.is_audition) {
                                t.setData({
                                    is_update: !1,
                                    is_buy: !1,
                                    is_show: !1,
                                    is_vshow: !0
                                }), t.videoInit(d, r);
                                break;
                            }
                            if (0 == r.is_audition) {
                                console.log("1111111111111111111111"), e.default.alert({
                                    title: "提示",
                                    message: "请升级会员或者购买课程",
                                    context: t
                                }).then(function() {}), t.setData({
                                    is_update: !0,
                                    is_buy: !0
                                });
                                break;
                            }
                            break;

                          case 2:
                            t.setData({
                                is_update: !1,
                                is_buy: !1
                            }), t.videoInit(d, r);
                            break;

                          case 3:
                          case 4:
                            t.setData({
                                is_update: !1,
                                is_buy: !1
                            }), t.videoInit(d, r);
                        }
                        wx.setStorageSync("orderErrReturnUrl", n.mini_not_permit_link);
                    } else wx.showToast({
                        title: "视频加载出错",
                        icon: "none",
                        duration: 1500
                    }), console.error(i);
                },
                fail: function(t) {
                    console.error(t);
                }
            });
        },
        videoInit: function(t, e) {
            this.setData({
                is_show: !1,
                is_vshow: !0,
                active: e.id
            }), this.viewPermission(e.id, t, e.course_id, e.is_audition), this.isMp4(this.data.vedioSrc);
        },
        viewPermissionBtn: function(t) {
            var e = t.currentTarget.dataset, i = e.item.id, o = e.index, a = e.item.course_id, s = e.item.is_audition;
            this.viewPermission(i, o, a, s);
        },
        viewPermission: function(t, e, i, o) {
            var a = this;
            this.setData({
                is_show: !0,
                is_vshow: !1,
                islive: !1
            }), this.setData({
                chapter_id: t,
                active: t
            });
            var n = {
                goods_id: this.data.goods_id,
                chapter_id: t
            }, r = s.getNetAddresss("plugin.video-demand.api.video-course-goods.get-video-address");
            s._getNetWork({
                url: r,
                data: n,
                success: function(s) {
                    if (1 == s.data.result) {
                        var n = s.data.data;
                        a.viewType(n.watch, e, n.video_address, i, t, o, n.video_cover_address, n.is_try, n.try_time);
                    } else console.error(s);
                },
                fail: function(t) {
                    console.error(t);
                }
            });
        },
        viewType: function(t, e, i, o, a, s, n, r, d) {
            var u = this;
            if (1 == r && 1 == t && 1 == s) return console.log("执行1"), this.setData({
                coverImg: n,
                is_update: !1,
                is_buy: !1,
                isLook: !0,
                is_vshow: !0,
                is_show: !1
            }), void this.videoPlay(e, i, o, a, n);
            if (2 != t && 3 != t && 4 != t && 1 == r && d > 0) return console.log("执行2"), this.setData({
                is_update: !1,
                is_buy: !1,
                isLook: !0
            }), this.videoPlay(e, i, o, a, n), void (this.data.is_try_time || (this.data.is_try_time = setTimeout(function() {
                u.setData({
                    islive: !1,
                    is_show: !0,
                    is_vshow: !1
                }), u.isNoPlay();
            }, 1e3 * d)));
            switch (console.log("执行3"), this.setData({
                vedioSrc: i,
                coverImg: n
            }), this.isMp4(this.data.vedioSrc), t) {
              case 0:
                this.setData({
                    is_vshow: !1,
                    is_show: !0
                }), this.isNoPlay();
                break;

              case 1:
                if (1 == s) {
                    this.setData({
                        is_update: !1,
                        is_buy: !1,
                        isLook: !0,
                        is_vshow: !0,
                        is_show: !1
                    }), this.videoPlay(e, i, o, a, n);
                    break;
                }
                if (0 == s) {
                    this.isNoPlay();
                    break;
                }
                break;

              case 2:
                this.setData({
                    is_update: !1,
                    is_buy: !1,
                    isLook: !0
                }), this.videoPlay(e, i, o, a, n);
                break;

              case 3:
              case 4:
                this.setData({
                    is_update: !1,
                    is_buy: !1,
                    isLook: !0
                }), this.videoPlay(e, i, o, a, n);
            }
        },
        isNoPlay: function() {
            this.setData({
                is_update: !0,
                is_buy: !0,
                isLook: !1
            }), this.data.styleSetOptions.buy_now_button && this.openBuyDialog();
        },
        openBuyDialog: function() {
            this.data.is_try_time && (clearTimeout(this.data.is_try_time), this.data.is_try_time = null), 
            console.log("222222222222222"), e.default.alert({
                title: "提示",
                message: "请升级会员或者购买课程",
                context: this
            }).then(function() {});
        },
        videoTimeupdate: function(t) {
            this.data.viewingDuration += .25, console.log("event", this.data.viewingDuration), 
            this.data.viewingDuration > 3 && this.data.videoEventDom.pause();
        },
        videoPlay: function(t, e, i, o, a) {
            this.setData({
                is_show: !1,
                is_vshow: !0,
                active: o,
                vedioSrc: e,
                coverImg: a
            }), this.isMp4(this.data.vedioSrc), s._getNetWork({
                url: s.getNetAddresss("plugin.video-demand.api.video-demand-member.set-watch-history"),
                data: {
                    course_id: i,
                    chapter_id: o
                },
                success: function(t) {}
            });
        },
        isMp4: function(t) {
            var e = this;
            "" != t && (t.endsWith(".mp4") || t.endsWith(".MP4") ? (this.setData({
                isVideo: !0,
                is_show: !1
            }), this.data.videoEventDom || (this.data.videoEventDom = wx.createVideoContext("myVideo"), 
            this.data.videoEventDom.play(), setTimeout(function() {
                e.data.videoEventDom.pause();
            }, 5e3), console.log("oooooooooooooooo", this.data.videoEventDom))) : this.setData({
                isVideo: !1,
                is_show: !1
            }));
        },
        study: function() {
            this.setData({
                is_show: !1,
                is_vshow: !0
            });
        },
        update: function() {
            if (this.data.ios_virtual_pay) return wx.showModal({
                title: "提示",
                content: "十分抱歉，由于相关规定，你暂时无法在这里充值！",
                confirmText: "知道了",
                showCancel: !1,
                success: function(t) {
                    t.confirm && console.log(t);
                }
            }), !1;
            wx.navigateTo({
                url: "/packageA/member/course/CourseMemberUpdate/CourseMemberUpdate?goods_id=" + this.data.goods_id
            });
        },
        buy: function() {
            return this.data.HasBuyPermissions ? (wx.showModal({
                title: "提示",
                content: this.data.HasBuyPermissions,
                confirmText: "确定",
                showCancel: !1,
                success: function(t) {}
            }), !1) : this.data.ios_virtual_pay ? (wx.showModal({
                title: "提示",
                content: "十分抱歉，由于相关规定，你暂时无法在这里充值！",
                confirmText: "知道了",
                showCancel: !1,
                success: function(t) {
                    t.confirm && console.log(t);
                }
            }), !1) : this.data.vip_level_status && 1 == this.data.vip_level_status.status ? (wx.showToast({
                title: this.data.vip_level_status.tips,
                icon: "none",
                duration: 1500
            }), !1) : void wx.navigateTo({
                url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&total=1&optionsId=&goodsId=" + this.data.goods_id
            });
        },
        toMyCourse: function() {
            wx.navigateTo({
                url: "/packageA/member/course/CourseMy/CourseMy"
            });
        },
        toHome: function() {
            wx.navigateTo({
                url: "/packageG/index/index"
            });
        },
        pushGoodGoto: function(t) {
            var e = t.currentTarget.dataset.item;
            wx.navigateTo({
                url: "/packageA/detail_v2/detail_v2?id=" + e.id
            });
        },
        chooseMoney: function(t) {
            var e = t.currentTarget.dataset.money;
            this.setData({
                rewardMoney: e
            });
        },
        moneyInput: function(t) {
            this.setData({
                rewardMoney: t.detail
            });
        },
        confirmReward: function() {
            console.log(this.data.rewardMoney), s._isTextEmpty(this.data.rewardMoney) ? wx.showToast({
                title: "请输入或选择打赏金额",
                icon: "none",
                duration: 1500
            }) : this.data.rewardMoney <= 0 ? wx.showToast({
                title: "打赏金额必须大于0",
                icon: "none",
                duration: 1500
            }) : this.getPayData();
        },
        getPayData: function() {
            var t = this;
            s._getNetWork({
                url: s.getNetAddresss("plugin.video-demand.api.lecturer-reward.pay-type"),
                success: function(e) {
                    if (1 === e.data.result) {
                        var i = e.data.data.buttons;
                        t.setData({
                            showPopup: !1,
                            btnData: i,
                            rewardPayshow: !0
                        });
                    } else console.error(e);
                },
                fail: function(t) {
                    console.error(t);
                }
            });
        },
        payPost: function(t) {
            var i = this;
            this.setData({
                rewardPayshow: !1
            });
            var o, a, n = null;
            t.currentTarget ? (o = t.currentTarget.dataset.info.value, a = t.currentTarget.dataset.info.name, 
            n = t.currentTarget.dataset.info) : (o = t.value, a = t.name, n = t);
            var r = {
                pay_method: o,
                amount: this.data.rewardMoney,
                pay_name: a,
                goods_id: this.data.goodsId,
                trick_id: this.data.stickid,
                client_type: "2"
            };
            s._getNetWork({
                url: s.getNetAddresss("plugin.video-demand.api.lecturer-reward.run-reward-pay"),
                data: r,
                success: function(t) {
                    1 === t.data.result ? (i.handleOtherPay(n, o, t.data.data), i.onClose()) : e.default.alert({
                        title: "提示",
                        message: t.data.msg,
                        context: i
                    }).then(function() {});
                },
                fail: function(t) {
                    console.error(t);
                }
            });
        },
        moneyShow: function() {
            this.setData({
                showPopup: !0
            });
        },
        openInputReward: function() {
            this.setData({
                showPopup: !1,
                rewardMoneyBtn: !0
            });
        },
        onClose: function() {
            this.setData({
                showPopup: !1,
                rewardMoneyBtn: !1,
                actionSheetShow: !1,
                questionnaire_is_open: !1
            });
        },
        openActionSheet: function() {
            this.setData({
                showPopup: !1,
                actionSheetShow: !0
            });
        },
        previewImage: function(t) {
            var e = t.target.dataset.src;
            wx.previewImage({
                current: e,
                urls: [ e ]
            });
        },
        footSet: function() {
            if (this.data.isGoods) {
                var t = s.getNetAddresss("member.member-history.store");
                t += "&goods_id=" + this.properties.goodsId + "&mark=" + this.properties.mark + "&mark_id=" + this.properties.mark_id, 
                s._getNetWork({
                    showToastIn: !1,
                    url: t,
                    success: function(t) {
                        var e = t.data;
                        1 == e.result ? console.log(e) : wx.showToast({
                            icon: "none",
                            title: e.msg,
                            duration: 1500
                        });
                    },
                    fail: function(t) {}
                });
            }
        },
        onChange: function(t) {
            console.log(this.data.activeNames), this.setData({
                activeNames: t.detail
            }), console.log(this.data.activeNames);
        },
        rewardPayshowClose: function() {
            this.setData({
                rewardPayshow: !1
            });
        },
        rewardPayCancelBtn: function() {
            this.setData({
                rewardPayshow: !1
            });
        },
        onChangecountDown: function(t) {
            this.setData({
                timeData: t.detail
            });
        },
        countDownFinish: function() {
            this.setData({
                iscCountDownFinish: !0
            });
        },
        toGroupCode: function() {
            "home" === this.data.goods_plugin.group_code.jump_mode ? wx.reLaunch({
                url: "/packageE/groupCode/groupCode"
            }) : this.data.goods_plugin.group_code.code_mini_url && wx.navigateTo({
                url: this.data.goods_plugin.group_code.code_mini_url
            });
        },
        toAdvertising: function() {
            console.log(this.data.advertising, "advertisingadvertising"), wx.navigateTo({
                url: this.data.advertising.min_link
            });
        }
    }
});